package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.dao.FootXfDao;
import org.footballmanager.pandafootball.system.domain.FootXf;
import org.footballmanager.pandafootball.system.form.impl.FootXfSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootXfMapper;
import org.footballmanager.pandafootball.system.service.FootGpService;
import org.footballmanager.pandafootball.system.service.FootUserPersonService;
import org.footballmanager.pandafootball.system.service.FootXfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootXfServiceImpl extends ServiceImpl<FootXf> implements FootXfService {
	@Autowired
	private FootXfDao footXfDao;
    @Autowired
    private FootXfMapper footXfMapper;
	@Autowired
	private FootGpService footGpService;
	@Autowired
	private FootUserPersonService footUserPersonService;
	
	@Override
	@Transactional
	public int save(FootXf t, HttpServletRequest request) {
		//删除挂牌信息
		footUserPersonService.deletePersonFromMyTeam(t.getUserId(), t.getQyId(), t.getXycs(), request);
		footGpService.deleteByQyidAndUserId(t.getQyId(), t.getUserId(), request);
		return super.save(t, request);
	}

	@Override
	public List<FootXf> findXfByTimeDesc() {
		return this.findall(new FootXfSearchForm());
	}

	/**
	 * 根据用户主键获取下放记录列表
	 *
	 * @param userId 用户主键
	 * @return 下放记录列表
	 */
	@Override
	public List<FootXf> findByUserId(Long userId) {
		Example example = new Example(FootXf.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(Constants.USERID, userId);
		return this.findall(example);
	}

    /**
     * 翻页方法
     *
     * @param footXfSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootXf> findall(FootXfSearchForm footXfSearchForm) {
        return footXfMapper.findall(footXfSearchForm);
    }

	/**
	 * 根据主键获取记录
	 * @param id 主键
	 * @return 记录
	 */
	@Override
	public FootXf get(Serializable id) {
		return footXfMapper.findById(id);
	}

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footXfSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootXfSearchForm footXfSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footXfSearchForm);
        Page<FootXf> page = PageHelper.startPage(footXfSearchForm.getPageIndex(), footXfSearchForm.getPageSize());
        List<FootXf> list = this.findall(footXfSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footXfSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootXfSearchForm footXfSearchForm, HttpServletRequest request) {
        if(footXfSearchForm.getSelectIds() != null){
            String[] result = footXfSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this .delete(Long.parseLong(id), request);
            }
        }
    }
}
